<template>
	<view class="">
		<template v-for="(item,index) of data">
			<!-- 工作 -->
			<jobItemComponent :info="item" v-if="item.task_type == 'task_job' || item.task_type == 'task_medical'" @longpress.stop="onLongPress(item.collect_id)">
			</jobItemComponent>
			<!-- 住房 -->
			<houseItemComponent :info="item" v-if="item.task_type == 'task_house'"  @longpress.stop="onLongPress(item.collect_id)"></houseItemComponent>
		</template>
	</view>
</template>

<script>
	import jobItemComponent from "@/components/jobItemComponent/index.vue"
	import houseItemComponent from "@/components/houseItemComponent/index.vue"
	import {
		pageCollect,
		removeCollect
	} from "@/api/collect.js"
	export default {
		components: {
			jobItemComponent,
			houseItemComponent
		},
		data() {
			return {
				data: [],
				page: {
					page_size: 7,
					page_number: 1,
					total: 0
				},
				status: "loadmore"
			}
		},
		onShow() {
			this.data = []
			this.page = {
				page_size: 7,
				page_number: 1,
				total: 0
			}
			this.status = "loadmore"
			this.getData()
		},
		onReachBottom() {
			this.getData()
		},
		methods: {
			onLongPress(id){
				uni.showModal({
					title: "取消收藏",
					content: "确定取消收藏吗？",
					success: (res) => {
						if(res.confirm){
							removeCollect({id})
							.then(res => {
								this.data = []
								this.page = {
									page_size: 7,
									page_number: 1,
									total: 0
								}
								this.status = "loadmore"
								this.getData()
							})
						}
					}
				})
			},
			getData() {
				if (this.status != "nomore") {
					this.status = "loading"
				} else {
					return
				}
				pageCollect({
						...this.page,
					})
					.then(res => {
						const data = (res.list || []).map(item => {
							try {
								item.images = JSON.parse(item.images)
							} catch (e) {
								item.images = []
							}
							try {
								item.tag = JSON.parse(item.tag)
							} catch (e) {
								item.tag = []
							}
							return item
						})
						this.page.total = res.total
						this.data.push(...data)
						console.log(this.data)
						if (this.data.length == res.total) {
							this.status = "nomore"
						} else {
							this.page.page_number += 1
							this.status = "loadmore"
						}
					})
					.catch(e => {
						this.status = "loadmore"
					})
			}
		}
	}
</script>

<style>
	page {
		background: #649CF6 linear-gradient(360deg, #fff 0%, #ECF3FF 60%, #F1F6FF 96%, #F1F6FF 100%);
	}
</style>